﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/3/22 星期五 0:52:44
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.EntityType;
using Sector7.Framework.DB;
using System.Collections;

namespace Sector7.Framework.DAO.Standard
{
    /// <summary>
    /// 数据元值域项数据访问对象
    /// </summary>
    public class ValueDomainItemDAO : DAOBase<S7_ValueDomainItem>, IValueDomainItemDAO
    {
        #region Public functions
        public override object Create(S7_ValueDomainItem item)
        {
            string sql = @"
                INSERT INTO S7_ValueDomainItem
                        (DomainItemValue
                        ,DomainItemImplication
                        ,DomainItemDescription
                        ,DomainId)
                    VALUES
                        (@DomainItemValue
                        ,@DomainItemImplication
                        ,@DomainItemDescription
                        ,@DomainId)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(item.DomainItemValue, "DomainItemValue"),
                    DBUtil.CreateSqlParameter(item.DomainItemImplication, "DomainItemImplication"),
                    DBUtil.CreateSqlParameter(item.DomainItemDescription, "DomainItemDescription"),
                    DBUtil.CreateSqlParameter(item.DomainId, "DomainId"));
            }
        }

        public int Create(List<S7_ValueDomainItem> items)
        {
            string sql = @"
                INSERT INTO S7_ValueDomainItem
                        (DomainItemValue
                        ,DomainItemImplication
                        ,DomainItemDescription
                        ,DomainId)
                    VALUES
                        (@DomainItemValue
                        ,@DomainItemImplication
                        ,@DomainItemDescription
                        ,@DomainId)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteBatchNonQuery(sql,
                    items,
                    "DomainItemValue",
                    "DomainItemImplication",
                    "DomainItemDescription",
                    "DomainId");
            }
        }

        public List<S7_ValueDomainItem> ReadAll(string domainId)
        {
            string sql = @"
                SELECT *
                    FROM S7_ValueDomainItem
                    WHERE DomainId = @DomainId";

            List<S7_ValueDomainItem> items = new List<S7_ValueDomainItem>();

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(domainId, "DomainId"));

                while (reader.Read())
                {
                    object item = new S7_ValueDomainItem();
                    DBUtil.ReadData(reader, item);

                    items.Add(item as S7_ValueDomainItem);
                }
            }

            return items;
        }
        #endregion
    }
}
